<template>
  <div class="login-wrap">
    <div class="ms-login">
      <div class="ms-title">旅游后台管理系统</div>
      <el-form class="ms-content" :model="param" :rules="rules" ref="loginForm" label-width="0px">
        <el-form-item prop="username">
          <el-input v-model="param.username" placeholder="username">
            <template #prepend>
              <el-button icon="el-icon-user"></el-button>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input type="password" placeholder="password" v-model="param.password" @keyup.enter="submitForm">
            <template #prepend>
              <el-button icon="el-icon-lock"></el-button>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="address">
          <el-input v-model="param.address" placeholder="address">
            <template #prepend>
              <el-button icon="el-icon-location-information"></el-button>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="phone">
          <el-input v-model="param.phone" placeholder="phone">
            <template #prepend>
              <el-button icon="el-icon-mobile-phone"></el-button>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="type">
          <el-radio-group v-model="param.type">
            <el-radio :label="0">管理员</el-radio>
            <el-radio :label="1">普通用户</el-radio>
            <el-radio :label="2">vip用户</el-radio>
          </el-radio-group>
        </el-form-item>
        <p class="login-tips"><router-link to="/login"><span>已有账号？登录</span></router-link> </p>
        <div class="login-btn">
          <el-button type="primary" @click="submitForm" data-test="loginBtn">注册</el-button>
        </div>
      </el-form>
    </div>
  </div>
</template>
<script>
import { reactive, unref, ref } from "vue";
import { useRoute, useRouter } from "vue-router";
import { ElMessage } from "element-plus";
import request from "../../utils/request";
export default {
  setup() {
    const param = reactive({
      username: "",
      password: "",
      address: "",
      phone: "",
      type: 0,//默认管理员
      status:1//用户状态正常
    });
    const rules = {
      username: [
        {
          required: true,
          message: "请输入用户名",
          trigger: "blur",
        },
      ],
      password: [
        { required: true, message: "请输入密码", trigger: "blur" },
        { min: 3, max: 10, message: "密码位数只能在3~10之间", trigger: "blur" },
      ],
      address: [
        {
          required: true,
          message: "请输入地址",
          trigger: "blur",
        },
      ],
      phone: [
        {required: true,message: "请输入手机号",trigger: "blur"},
        { pattern: /^1[3456789]\d{9}$/, message: '手机号码格式不正确', trigger: 'blur' }
      ],
    };
    const loginForm = ref(null);
    const router = useRouter();
    const submitForm = () => {
      loginForm.value.validate((valid) => {
        if (valid) {
          request
            .request({
              url: "/user/add",
              method: "post",
              data: {
                username: param.username,
                password: param.password,
                address: param.address,
                phone: param.phone,
                type: param.type,
                status:1
              },
            })
            .then((res) => {
              if (res.data.code == "505" && res.data.msg == "fail") {
                ElMessage({
                message: `该用户名已被注册`,
                type: 'error',
              })
              }
              else{
                if (res.data.code == "200" && res.data.msg == "success") {
                ElMessage({
                  message: `恭喜,${param.username}注册成功`,
                  type: "success",
                });
                router.push("/login");
              }

              }
              
            })
            .catch((e) => {
              console.log(e);
              ElMessage({
                message: `注册失败,请重试`,
                type: 'error',
              })
            });
        } else {
          ElMessage({
            message: `注册失败,请重试`,
            type: "error",
          });
        }
      });
    };
    return {
      loginForm,
      submitForm,
      param,
      rules,
    };
  },
};
</script>
<style lang="scss" scoped>
.login-wrap {
  position: relative;
  width: 100%;
  height: 100%;
  background-size: 100%;
}

.ms-title {
  width: 100%;
  line-height: 50px;
  text-align: center;
  font-size: 20px;
  color: #fff;
  border-bottom: 1px solid #ddd;
}

.ms-login {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 350px;
  margin: -250px 0 0 -175px;
  border-radius: 5px;
  background: rgba(255, 255, 255, 0.3);
  overflow: hidden;
}

.ms-content {
  padding: 30px 30px;
}

.login-btn {
  text-align: center;
}

.login-btn button {
  width: 100%;
  height: 36px;
  margin-bottom: 10px;
}

.login-tips span {
  font-size: 12px;
  line-height: 30px;
  color: #fff;
}
</style>
  